package com.zhouyibike.uu.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zhouyibike.uu.R;
import com.zhouyibike.uu.entity.Cityselectbean;

import java.util.ArrayList;

public class MyrecyAdapter1 extends RecyclerView.Adapter<MyrecyAdapter1.ViewHolder> implements View.OnClickListener {
    private ArrayList<Cityselectbean> citylistall=new ArrayList<>();
    private Context context;
    public MyrecyAdapter1(ArrayList<Cityselectbean> citylistall,Context context) {
        this.citylistall = citylistall;
        this.context = context;
    }

    private OnItemClickListener mOnItemClickListener = null;

    //define interface
    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_citylist, viewGroup, false);
        ViewHolder vh = new ViewHolder(view);
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        viewHolder.mTextView.setText(citylistall.get(position).getKey().getCode());
        MyrecyAdapter2 adapter2=new MyrecyAdapter2((ArrayList<Cityselectbean.KeyBean.ListBean>) citylistall.get(position).getKey().getList());
        //创建线性布局
        StaggeredGridLayoutManager mLayoutManager = new StaggeredGridLayoutManager(2 , StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
        //垂直方向
//        mLayoutManager.setOrientation(OrientationHelper.VERTICAL);
        //给RecyclerView设置布局管理器
        viewHolder. recycle2.setLayoutManager(mLayoutManager);
        //创建适配器，并且设置
        viewHolder. recycle2.setAdapter(adapter2);
        //将position保存在itemView的Tag中，以便点击时进行获取
        viewHolder.itemView.setTag(position);
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return citylistall.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;
        public RecyclerView recycle2;
        public ViewHolder(View view) {
            super(view);
            mTextView = (TextView) view.findViewById(R.id.tv_tv);
            recycle2=view.findViewById(R.id.recycle2);
        }
    }
}
